import { Component, OnInit } from '@angular/core';

import { remove as _remove } from 'lodash';

import { Suggestion } from './rxjs-suggestion.model';

import { RxjsGithubService } from './rxjs-github.service';

@Component({
    selector: 'guide-rxjs',
    templateUrl: './rxjs.component.html',
    styleUrls: ['./rxjs.component.css']
})
export class RxjsComponent implements OnInit {
    suggestions: Suggestion[];
    showList: Suggestion[];
    constructor(private githubService: RxjsGithubService) { }

    ngOnInit() {
        this.getSuggestions();
    }
    refresh() {
        this.getSuggestions();
        return false;
    }
    getNewOne(suggestion) {
        _remove(this.showList, item => item.login === suggestion.login);
        _remove(this.suggestions, item => item.login === suggestion.login);
        this.showList.push(this.suggestions[Math.floor(Math.random() * this.suggestions.length)]);

    }
    trackByLogin(index: number, suggestion: Suggestion) {
        return suggestion.login;
    }
    private getSuggestions() {
        this.githubService.getSuggestions()
            .subscribe(
            suggestions => {
                this.suggestions = suggestions;
                this.showList = this.suggestions.slice(0, 3);
            });
    }
}
